﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Xml.Linq;
using System.Collections.Generic;

namespace WebApplication1
{
    public partial class client : System.Web.UI.Page
    {
        private Int32 clId;
        private Int32 agId;
        private Int32 cert;
        private Int32 i = 0; private String lab;
        private int role;
        private string name;
        private string surname;
        private int id;
        
        private string Сon = @"Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=Test;
Integrated Security=True;Pooling=False";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                id = Convert.ToInt32(Session["id"]);
                role = Convert.ToInt32(Session["role"]);
                name = Convert.ToString(Session["name"]);
                surname = Convert.ToString(Session["surname"]);
                Label2.Text =Convert.ToString(id);
                agId = Convert.ToInt32(Label2.Text);
                DropDownList1.Items.Add(Label2.Text);
                Session.Remove("id");
                Session.Remove("role");
                Session.Remove("name");
                Session.Remove("surname");
                Session["id"] = id;
                Session["name"] = name;
                Session["surname"] = surname;
                Session["role"] = role;
                DropDownList1.SelectedIndex=0;
                Label2.Text=DropDownList1.SelectedValue;
            }
            catch (Exception ex)
            {
                Label2.Text = ("Ошибка при вставке данных: " + ex.Message);
            }
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextBox12.Text = "";
            CheckBox1.Checked = false;
            CheckBox2.Checked = false;
            CheckBox3.Checked = false;
            CheckBox4.Checked = false;
            CheckBox5.Checked = false;
            CheckBox6.Checked = false;
            HyperLink1.Visible = false;
            Panel1.Visible = true;
            //Label1.Text = Convert.ToString(clId);
            Label3.Visible=false;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            cert = Convert.ToInt32(TextBox1.Text);
            Panel1.Visible = false;
            Panel2.Visible = true;
            SqlConnection connection = new SqlConnection(Сon);
            connection.Open();
            try
            {
                clId= Convert.ToInt32(GridView1.SelectedDataKey.Value);
                SqlCommand сommand1 = new SqlCommand("insert into Flat (Certificate,IdClient,Adress,LostFloor,Rooms,Square,Year,Price)" +
                    "VALUES ('" + Convert.ToInt32(TextBox1.Text) + "','" + clId + "','" + TextBox2.Text + "','" + CheckBox1.Checked + "','" + Convert.ToInt32(TextBox4.Text) + "','" + Convert.ToDouble(TextBox5.Text) + "','" + Convert.ToInt32(TextBox6.Text) + "','" + Convert.ToInt32(TextBox7.Text) + "')", connection);
                сommand1.ExecuteNonQuery();


            }
            catch (SqlException ex)
            {
               Label3.Text=("Ошибка при вставке данных: " + ex.Message);
            }
            connection.Close();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           agId=Convert.ToInt32(DropDownList1.SelectedValue);
                SqlConnection connection = new SqlConnection(Сon);
            connection.Open();
            try
            {
                SqlCommand сommand1 = new SqlCommand("insert into Contract (IdFlat,IdAgent,Fire,Water,Wear,Problem,Loot,Date,Sum)" +
                    "VALUES ('" + Convert.ToInt32(TextBox1.Text) + "','" + agId + "','" + CheckBox3.Checked + "','" + CheckBox4.Checked + "','" + CheckBox2.Checked + "','" + CheckBox5.Checked + "','" + CheckBox6.Checked + "','" + Convert.ToString(Calendar1.SelectedDate) + "','" + Convert.ToInt32(TextBox12.Text) + "')", connection);
                сommand1.ExecuteNonQuery();
               

            }
            catch (SqlException ex)
            {
                Label3.Text = ("Ошибка при вставке данных: " + ex.Message);
                

            }
            connection.Close();
            Panel2.Visible = false;
            //Label3.Text = "Новый договор успешно добавлен!";
            Label3.Visible = true;
            
        }


        }
        

        
    }
